/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: kfa_demux.c
 *
 * Purpose: implementation a demuxer to demux kfa(key frame animation) file
 *
 * Developer:
 *   wen.gu , 2022-03-04
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include "kfa_demux.h"

#include <string.h>

#include "kfa_io.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define LOGD printf
#define LOGE printf
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/

/******************************************************************************
 **    inner FUNCTION DEFINITIONS
 ******************************************************************************/

/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
KfaDemux::KfaDemux()
{

}

KfaDemux::~KfaDemux()
{

}

bool KfaDemux::setDataSource(const std::string& src_file)
{
    if (src_fp_)
    {
        return false;
    }

    src_fp_ = fopen(src_file.c_str(), "rb");

    if (!src_fp_)
    {
        LOGE("open file(%s) failed(%s)\n", src_file.c_str(), strerror(errno));
        return false;
    }

    size_t ret = kfa_read_bin(src_fp_, &header_, sizeof(header_));

    if (ret != sizeof(header_))
    {
        fclose(src_fp_);
        src_fp_ = nullptr;
        LOGE("reader kfa header failed(header size: %d, read size: %d)\n", sizeof(header_), ret);
        return false;
    }

    if (header_.fourcc != KFA_TYPE_FCC)
    {
        fclose(src_fp_);
        src_fp_ = nullptr;
        LOGE("not a kfa file(kfa: 0x%0x, read:0x%08x)\n", KFA_TYPE_FCC, header_.fourcc);
        return false;        
    }

    return true;

}

bool KfaDemux::getHeader(kfa_header& header)
{
    header = header_;
    return true;
}
bool KfaDemux::reset()
{
    int pos = fseek(src_fp_, sizeof(header_), SEEK_SET);    

    return pos == 0;
}

bool KfaDemux::read(uint8_t* & out_block, uint32_t& block_size)
{
    size_t ret = 0;
#if 0
    ret = kfa_read_value(src_fp_, out_block.frame_count);
    if (ret != 1)
    {
        out_block.frame_count = 0;
        return false;
    }


    ret = kfa_read_value(src_fp_, out_block.raw_size);
    if (ret != 1)
    {
        out_block.raw_size = 0;
        return false;
    }
#endif
    ret = kfa_read_value(src_fp_, block_size);
    if (ret != 1)
    {
        block_size = 0;
        return false;
    }
    
    if (block_size)
    {
        out_block = new uint8_t[block_size];
        ret = kfa_read_bin(src_fp_, out_block, block_size);

        return block_size == ret;
    }

    return false;    
}